﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace KorTransLib
{
    public class PolygonArea
    {
        public static bool CalculateCentroid(List<UTMCoordinate> utms, out double yukarı, out double sağa)
        {
            yukarı = sağa = 0;
            if (utms.Count <= 2)
            {
                return false;
            }
            string utmZone = "";
            foreach (UTMCoordinate c in utms)
            {
                if (utmZone.Length == 0)
                    utmZone = c.Zone;
                else
                {
                    //if collection contains utm coordinates spanning more than one UTM slice zone then return false.
                    if (utmZone != c.Zone)
                    {
                        return false;
                    }
                }
            }

            List<UTMCoordinate> lst = new List<UTMCoordinate>(utms);
            lst.Add(utms[0]);
            double area = 0;
            for (int i = 0; i < utms.Count; i++)
            {
                yukarı += (lst[i].Yukarı + lst[i + 1].Yukarı) * (lst[i].Yukarı * lst[i + 1].Sağa - lst[i + 1].Yukarı * lst[i].Sağa);
                sağa += (lst[i].Sağa + lst[i + 1].Sağa) * (lst[i].Yukarı * lst[i + 1].Sağa - lst[i + 1].Yukarı * lst[i].Sağa);
                area += lst[i].Yukarı * lst[i + 1].Sağa - lst[i + 1].Yukarı * lst[i].Sağa;
            }
            area /= 2.0;
            area = Math.Abs(area);
            yukarı /= 6 * area;
            sağa /= 6 * area;
            return true;
        }

        public static bool CalculateArea(List<UTMCoordinate> utms, out double area)
        {
            if (utms.Count <= 2)
            {
                area = 0;
                return false;
            }
            string utmZone = "";
            foreach (UTMCoordinate c in utms)
            {
                if (utmZone.Length == 0)
                    utmZone = c.Zone;
                else
                {
                    //if collection contains utm coordinates spanning more than one UTM slice zone then return false.
                    if (utmZone != c.Zone)
                    {
                        area = 0;
                        return false;
                    }
                }
            }
            area = 0.0d;

            List<UTMCoordinate> lst = new List<UTMCoordinate>(utms);
            lst.Add(utms[0]);

            for (int i = 0; i < utms.Count; i++)
            {
                area += lst[i].Yukarı * lst[i + 1].Sağa - lst[i + 1].Yukarı * lst[i].Sağa;
            }
            area /= 2.0;
            area = Math.Abs(area);
            return true;
        }
    }
}
